local canmou = fk.CreateSkill{
    name = "jinld__canmou",
}

Fk:loadTranslationTable{
    ["jinld__canmou"] = "参谋",
    [":jinld__canmou"] = "与你势力相同的角色每回合首次使用普通锦囊牌时，你可以展示一张应变牌，令此牌触发相同的应变效果，然后若你是此牌的目标，你摸一张牌。",

    ["#jinld__canmou-invoke"] = "参谋：是否发动参谋，展示一张应变牌，令 %arg 触发相同的应变效果",

    ["$jinld__canmou1"] = "兢兢业业，竭心筹划。",
    ["$jinld__canmou2"] = "欲设此法，计谋二人。",
}

local H = require "packages/ol_hegemony/util"

canmou:addEffect(fk.PreCardUse,{
    anim_type = "support",
    can_trigger = function (self, event, target, player, data)
        if player:hasSkill(canmou.name) and H.compareKingdomWith(player, target) and data.card:isCommonTrick()
        and not player:isKongcheng() then
        local use_event = player.room.logic:getCurrentEvent():findParent(GameEvent.UseCard, true)
        if use_event == nil then return end
        local use_events = player.room.logic:getEventsOfScope(GameEvent.UseCard, 1, function (e)
          local use = e.data
          return use.from == target and use.card.type == data.card.type
        end, Player.HistoryTurn)
        return use_events[1].id == use_event.id
      end
    end,
    on_cost = function (self, event, target, player, data)
        local room = player.room
        local cards = {}
         for _, c in ipairs(player:getCardIds("h")) do
          local card = Fk:getCardById(c)
          if card ~= data.card then
          if table.find({"@fujia-public", "@kongchao-public", "@canqu-public", "@zhuzhan-public", "@heg_yibing-public"}, function(mark)
          return card.extra_data and card.extra_data.heg_variation and card.extra_data.heg_variation[1] == mark end) then
            table.insert(cards, card.id)
            end
          end
        end
        if #cards == 0 then return end
        return room:askToSkillInvoke(player, {skill_name = canmou.name, prompt = "#jinld__canmou-invoke:::"..data.card:toLogString()})
    end,
    on_use = function (self, event, target, player, data)
        local room = player.room
        local cards = {}
        for _, c in ipairs(player:getCardIds("h")) do
          local card = Fk:getCardById(c)
          if card ~= data.card then
          if table.find({"@fujia-public", "@kongchao-public", "@canqu-public", "@zhuzhan-public", "@heg_yibing-public"}, function(mark)
          return card.extra_data and card.extra_data.heg_variation and card.extra_data.heg_variation[1] == mark end) then
            table.insert(cards, card.id)
           end
         end
        end
        if #cards == 0 then return end
        local card = room:askToChooseCard(player,{
            target = player,
            flag = { card_data = {{ canmou.name, cards }} },
            skill_name = canmou.name,
        })
        room:showCards(card, player)
        data.extra_data = data.extra_data or {}
        data.extra_data.heg_variation_effects = data.extra_data.heg_variation_effects or {}
        table.insertIfNeed(data.extra_data.heg_variation_effects, Fk:getCardById(card).extra_data.heg_variation[2])
        if table.contains(data.tos, player) then
        player:drawCards(1,canmou.name)
      end
    end,
})

return canmou